import java.util.*;


public class Solution8 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param matrix int整型二维数组 
     * @return int整型ArrayList
     */
    public ArrayList<Integer> spiralOrder (int[][] matrix) {
        // write code here
         ArrayList<Integer> ret = new ArrayList<>();
         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return ret;
         } 
        int up = 0;
        int left = 0;
        int down = matrix.length - 1;
        int right = matrix[0].length-1;
       
        while(up <= down && left <= right) {
            for(int i = left;i<=right ;i++) {
                ret.add(matrix[up][i]);
            }
            up++;
            if(up > down) {
                break;
            }
            for(int i = up;i<= down;i++) {
                ret.add(matrix[i][right]);
            }
            right--;
            if(left > right) {
                break;
            }
            for(int i = right;i>=left;i--) {
                ret.add(matrix[down][i]);
            }
            down--;
            if(up > down) {
                break;
            }
            for(int i = down;i>=up;i--) {
                ret.add(matrix[i][left]);
            }
            left++;
            if(left > right) {
                break;
            }
        }
        return ret;
    }
}